---
title: "R Notebook"
output: html_notebook
---

This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code. 

Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Ctrl+Shift+Enter*. 
```{r}
library(e1071)
```

Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Ctrl+Alt+I*.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Ctrl+Shift+K* to preview the HTML file).
```{r}
dataPath<-"data/"
dat <- read.csv(paste0(dataPath,"test_sample.csv"))
```

```{r}
dat<-as.data.frame(dat)
dat$class<-as.factor(dat$class)
dat
```
```{r}
plot(dat$x,dat$y,col="orange",pch=16,ylim=c(0,1),xlim=c(0,1))
points(dat$x[dat$class==1],dat$y[dat$class==1],col="blue",pch=16)
```
```{r}
m1<-svm(class~.  , data=dat)
m1.pred<-predict(m1)
```
```{r}
plot(m1,dat)
```
```{r}
classAgreement(table(pred = m1.pred, true = dat[,"class"]))$diag 
```
```{r}
set.seed(1)
svmTuned <- tune.svm(class~., data = dat, gamma = 10^(-3:-1), cost = 5*(2:5))
summary(svmTuned)
```
```{r}
plot(svmTuned$best.model,dat)
```

```{r}
set.seed(1)
predict <- predict(svmTuned$best.model, dat[,!names(dat)=="class"])
classAgreement(table(pred = predict, true = dat[,"class"]))$diag
```
